﻿using Newtonsoft.Json;
using PIMS.DBModel;
using PIMS.Interface;
using PIMS.Models.Base;
using PIMS.Utils;
using PIMS.WorkPlugin.EAS;
using PIMS.WorkPlugin.ORM;
using PIMS.XmlModel;
using PIMS.YongXiang;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlTypes;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 星瀚系统接口测试.金蝶星瀚系统.QueryCartonInfoResponse;
using 金蝶星瀚系统;
using static Org.BouncyCastle.Math.EC.ECCurve;

namespace PIMS.WorkPlugin
{
    public partial class Form箱信息补传 : Form
    {
        private IDebugMsg debug = null;
        private ILog4net log = null;
        private string plant = "";

        private string offLinePort { get; set; }


        public Form箱信息补传()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 注：反射加载时会自动调用一次
        /// </summary>
        public void Init(IDebugMsg debugMsg, ILog4net log4net)
        {
            this.debug = debugMsg;
            this.log = log4net;
        }

        private void Form箱信息补传_Load(object sender, EventArgs e)
        {
            this.btn补传EAS.Enabled = false;
            this.btn补传WMS.Enabled = false;
           // this.btn补传MES.Enabled = false;

            try
            {
                PIMSConfigX pimsConfig= XmlHelper.XmlFileToObject<PIMSConfigX>(AppDomain.CurrentDomain.BaseDirectory
                    + "Config/PIMSConfig.xml");
                FeatureInfoX savedDataConfigX = XmlHelper.XmlFileToObject<FeatureInfoX>(AppDomain.CurrentDomain.BaseDirectory
                   + "Config/savedDataConfig.xml");
                DgvTools.dataGridView(this.dgv);
                DgvTools.AutoGenerateColumns(this.dgv, savedDataConfigX);

                this.plant = pimsConfig.GetAppValueByKey("plant");
                dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btn刷新_Click(object sender, EventArgs e)
        {            
            (new System.Threading.Tasks.Task(ThreadRefresh)).Start();
        }

        private void btn补传EAS_Click(object sender, EventArgs e)
        {
            if (this.dgv.SelectedRows.Count != 1)
            {
                MessageBox.Show("请选中一行再进行操作！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else if ("1" == this.dgv.SelectedRows[0].Cells["上传EAS标志"].Value.ToString())
            {
                MessageBox.Show("重复提交", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            this.btn补传EAS.Enabled = false;
            string boxId = this.dgv.SelectedRows[0].Cells["箱标"].Value.ToString();
            string msg = KingDee.Import_CartonInfo(boxId);
            this.debug.DebugMsg(boxId + " 补传EAS " + msg);
            this.log.Info(boxId + " 补传EAS " + msg);
            MessageBox.Show(msg, "补传EAS");

            (new System.Threading.Tasks.Task(ThreadRefresh)).Start();
        }

        private void btn补传WMS_Click(object sender, EventArgs e)
        {
            if (this.dgv.SelectedRows.Count != 1)
            {
                MessageBox.Show("请选中一行再进行操作！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else if ("1" == this.dgv.SelectedRows[0].Cells["上传WMS标志"].Value.ToString())
            {
                MessageBox.Show("重复提交", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            this.btn补传WMS.Enabled = false;
            string boxId = this.dgv.SelectedRows[0].Cells["箱标"].Value.ToString();
            if(String.IsNullOrEmpty(this.offLinePort))
            {
                this.offLinePort = this.plant;
            }
            //string msg= WMSApi.boxOfflineNotice(boxId, this.offLinePort);
            //this.debug.DebugMsg(boxId + $" 补传WMS({this.offLinePort}下线口) " + msg);
            //this.log.Info(boxId + $" 补传WMS({this.offLinePort}下线口) " + msg);
            //MessageBox.Show(msg, $" 补传WMS({this.offLinePort}下线口) ");

            (new System.Threading.Tasks.Task(ThreadRefresh)).Start();
        }

        private void ThreadRefresh()
        {
            this.Invoke(
             new Action(() => {
               this.dgv.DataSource = null;
              this.btn刷新.Enabled = false;
            }));

            //string sqlCmd = "select * from tb_saveddata where reserved7 is null or reserved10 is null or reserved12 is null order by productTime desc";
            string sqlCmd = "select * from tb_saveddata where reserved7 is null or reserved12 is null order by productTime desc";
            DataTable dt= SqlSugarHelper.QueryToDataTable<saveddata>(sqlCmd);

            this.Invoke(
             new Action(() => {
                this.dgv.DataSource= dt;
                DgvTools.dataGridViewAutoSize(this.dgv);
                this.btn刷新.Enabled = true;
            }));            
        }

        private void dgv_Click(object sender, EventArgs e)
        {
            if (this.dgv.SelectedRows.Count == 1)
            {
                if ("1" != this.dgv.SelectedRows[0].Cells["上传EAS标志"].Value.ToString())
                {
                    this.btn补传EAS.Enabled = true;
                }
                else
                {
                    this.btn补传EAS.Enabled = false;
                }

                //if ("1" != this.dgv.SelectedRows[0].Cells["上传WMS标志"].Value.ToString())
                //{
                //    this.btn补传WMS.Enabled = true;
                //}
                //else
                //{
                //    this.btn补传WMS.Enabled = false;
                //}

                //if ("1" != this.dgv.SelectedRows[0].Cells["上传MES标志"].Value.ToString())
                //{
                //    this.btn补传MES.Enabled = true;
                //}
                //else
                //{
                //    this.btn补传MES.Enabled = false;
                //}
            }
            else
            {
                this.btn补传EAS.Enabled = false;
                this.btn补传WMS.Enabled = false;
               // this.btn补传MES.Enabled = false;
            }
        }

        private void 查询_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                MessageBox.Show("请输入箱号");
                return;
            }
            BoxHistory carton;
            //Box carton;
            //string result = EASHelper.GetCartonInfo("cartonNumber", textBox1.Text, out carton);
            string result = KingDee.QueryCartonInfo("cartonNumber", textBox1.Text, out carton);
            if (result.Contains("Err"))
                MessageBox.Show(result);
            else
                MessageBox.Show(result);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox3.Text))
            {
                MessageBox.Show("请输入托号");
                return;
            }
            BoxHistory carton;
            string result = KingDee.QueryCartonInfo("palletNumber", textBox3.Text, out carton);
            if (result.Contains("Err"))
                MessageBox.Show(result);
            else
                MessageBox.Show(JsonConvert.SerializeObject(carton));
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string[] sTmps = new string[] { textBox1.Text, textBox2.Text };
            string sRet2 = KingDee.Import_CpRelation(sTmps, textBox3.Text);
            if (sRet2.Contains("Err"))
            {
                MessageBox.Show(sRet2);
            }
            else
            {
                MessageBox.Show("上传成功");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {

        }
    }
}
